package com.umeox.watch.moto.chat.db

import android.os.Parcel
import android.os.Parcelable
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.Ignore
import androidx.room.PrimaryKey

/**
 * Created by Rambo 2021/8/18
 * Describe:
 */
@Entity(tableName = "contact_info")
data class ContactInfo(
    @PrimaryKey(autoGenerate = true)
    var id: Long = 0,
    @ColumnInfo
    var friendId: String = "", //为群聊时对应群聊ID
    @ColumnInfo
    var isAdmin: Boolean = false,
    //1表示手机联系人，2表示设备好友
    //群组的话 0家庭群聊  1APP群聊  2设备群聊
    @ColumnInfo
    var type: Int = 0,
    @ColumnInfo
    var name: String = "",
    @ColumnInfo
    var avatar: String? = "",
    @ColumnInfo
    var lastContactTime: Long = 0L,
    @ColumnInfo
    var lastMessageId: String = "",
    @ColumnInfo
    var missCount: Int = 0,
    @ColumnInfo
    var createTime: Int = 0,
    @ColumnInfo
    var isGroup: Boolean = false //是否为群聊
) : Parcelable {

    constructor(parcel: Parcel) : this() {
        this.id = parcel.readLong()
        this.friendId = parcel.readString().toString()
        this.isAdmin = parcel.readByte().toInt() != 0
        this.type = parcel.readInt()
        this.name = parcel.readString().toString()
        this.avatar = parcel.readString()
        this.lastContactTime = parcel.readLong()
        this.lastMessageId = parcel.readString().toString()
        this.missCount = parcel.readInt()
        this.createTime = parcel.readInt()
        this.isGroup = parcel.readByte().toInt() != 0
    }

    override fun writeToParcel(dest: Parcel, flags: Int) {
        dest.writeLong(id)
        dest.writeString(friendId)
        dest.writeByte(if (isAdmin) 1 else 0)
        dest.writeInt(type)
        dest.writeString(name)
        dest.writeString(avatar)
        dest.writeLong(lastContactTime)
        dest.writeString(lastMessageId)
        dest.writeInt(missCount)
        dest.writeInt(createTime)
        dest.writeByte(if (isGroup) 1 else 0)
    }

    override fun describeContents(): Int {
        return 0
    }

    override fun equals(other: Any?): Boolean {
        if(this === other) return true
        if(other !is ContactInfo) return  false
        val that : ContactInfo = other
        if(type != that.type) return false
        return friendId == that.friendId
    }

    companion object CREATOR : Parcelable.Creator<ContactInfo> {
        override fun createFromParcel(source: Parcel): ContactInfo {
            return ContactInfo(source)
        }

        override fun newArray(size: Int): Array<ContactInfo?> {
            return arrayOfNulls(size)
        }

    }
}
